package cn.kgc.ssm.interceptors;

import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2024/5/21
 * @description    非法请求拦击
 */

public class LoginInterceptor  implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (ObjectUtils.isEmpty(user)){
            //将页面重定向到登录页
            response.sendRedirect(request.getContextPath()+"/admin/login.html");
            return  false;
        }

        return  true;
    }
}
